package com.ybkj.daijia.api.common.v1;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.api.member.ErrorConstant;
import com.ybkj.daijia.api.member.alipay.sign.Base64;
import com.ybkj.daijia.api.member.transport.ResultTransport;
import com.ybkj.daijia.api.weixin.tenpayv3.MD5Util;
import com.ybkj.daijia.common.Result;
import com.ybkj.daijia.config.ErrandApi;
import com.ybkj.daijia.config.PassengerApi;
import com.ybkj.daijia.config.SmsApi;
import com.ybkj.daijia.server.mc.Passenger;
import com.ybkj.daijia.server.mc.PassengerAccount;
import com.ybkj.daijia.server.model.Admin;
import com.ybkj.daijia.server.model.Coupon;
import com.ybkj.daijia.server.model.CouponRule;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.model.DriverChoseLevel;
import com.ybkj.daijia.server.model.DriverConsumedRecord;
import com.ybkj.daijia.server.model.DriverLevel;
import com.ybkj.daijia.server.model.DriverPaymentsRecord;
import com.ybkj.daijia.server.model.DriverRechargeRecord;
import com.ybkj.daijia.server.model.DriverRefuseRecord;
import com.ybkj.daijia.server.model.DriverTime;
import com.ybkj.daijia.server.model.FinancialRecord;
import com.ybkj.daijia.server.model.Order;
import com.ybkj.daijia.server.model.OrderFinishedInfo;
import com.ybkj.daijia.server.model.OrderStatus;
import com.ybkj.daijia.server.model.WeixinManagerXOrder;
import com.ybkj.daijia.server.sales.CouponRule.CouponType;
import com.ybkj.daijia.server.service.AdminService;
import com.ybkj.daijia.server.service.BrandService;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.CouponRuleService;
import com.ybkj.daijia.server.service.CouponService;
import com.ybkj.daijia.server.service.DriverChoseLevelService;
import com.ybkj.daijia.server.service.DriverConsumedRecordService;
import com.ybkj.daijia.server.service.DriverLevelService;
import com.ybkj.daijia.server.service.DriverPaymentsRecordService;
import com.ybkj.daijia.server.service.DriverRechargeRecordService;
import com.ybkj.daijia.server.service.DriverRefuseRecordService;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.DriverTimeService;
import com.ybkj.daijia.server.service.ErrandService;
import com.ybkj.daijia.server.service.FinancialRecordService;
import com.ybkj.daijia.server.service.FreightService;
import com.ybkj.daijia.server.service.MemberAccountService;
import com.ybkj.daijia.server.service.MemberConsumedRecordService;
import com.ybkj.daijia.server.service.MemberPaymentsRecordService;
import com.ybkj.daijia.server.service.MemberRechargeRecordService;
import com.ybkj.daijia.server.service.MemberService;
import com.ybkj.daijia.server.service.OrderFinishedInfoService;
import com.ybkj.daijia.server.service.OrderService;
import com.ybkj.daijia.server.service.OrderStatusService;
import com.ybkj.daijia.server.service.WeixinManagerXOrderService;
import com.ybkj.daijia.server.service.ZhuancheService;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.WebUtils;

@Controller
@RequestMapping(value = "log/v1/")
public class LogController {

    private static final Logger logger = LoggerFactory.getLogger(LogController.class);

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private DriverService driverService;

    @Autowired
    private DriverLevelService driverLevelService;

    @Autowired
    private MemberAccountService memberAccountService;

    @Autowired
    private DriverChoseLevelService driverChoseLevelService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private DriverTimeService driverTimeService;

    @Autowired
    private MemberConsumedRecordService memberConsumedRecordService;

    @Autowired
    private MemberRechargeRecordService memberRechargeRecordService;

    @Autowired
    private MemberPaymentsRecordService memberPaymentsRecordService;

    @Autowired
    private DriverConsumedRecordService driverConsumedRecordService;

    @Autowired
    private DriverRechargeRecordService driverRechargeRecordService;

    @Autowired
    private WeixinManagerXOrderService weixinManagerXOrderService;

    @Autowired
    private OrderFinishedInfoService orderFinishedInfoService;

    @Autowired
    private BrandService brandService;

    @Autowired
    private FinancialRecordService financialRecordService;

    @Autowired
    private OrderStatusService orderStatusService;

    @Autowired
    private DriverRefuseRecordService driverRefuseRecordService;

    @Autowired
    private CouponRuleService couponRuleService;

    @Autowired
    private CouponService couponService;

    @Autowired
    private ZhuancheService zhuancheService;

    @Autowired
    private ApplicationEventMulticaster applicationEventMulticaster;

    @Autowired
    private TaskExecutor taskExecutor;

    @Autowired
    private PassengerApi passengerApi;

    @Autowired
    private ErrandApi errandApi;

    @Autowired
    private ErrandService errandService;

    @Autowired
    private FreightService freightService;

    @Autowired
    private AdminService adminService;

    @Autowired
    private DriverPaymentsRecordService driverPaymentsRecordService;

    @Autowired
    private SmsApi smsApi;

    public static String getValue(HSSFCell hssfCell) {
        if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
            // 返回布尔类型的值
            return String.valueOf(hssfCell.getBooleanCellValue());
        } else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
            // 返回数值类型的值
            return String.valueOf(hssfCell.getNumericCellValue());
        } else {
            // 返回字符串类型的值
            return String.valueOf(hssfCell.getStringCellValue());
        }
    }

    @RequestMapping(value = "upload", method = RequestMethod.POST)
    public @ResponseBody
    ResultTransport upload(
        HttpServletRequest req,
        @RequestParam(required = true, value = "file") MultipartFile file,
        String deviceType) {

        logger.debug("recv deviceType:{}", deviceType);

        try {

            if (StringUtils.isNotBlank(deviceType) && null != file) {
                String bathPath = getBasePath(req);

                File dir = new File(bathPath, "log/" + deviceType);

                FileUtils.forceMkdir(dir);

                SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");

                String fileName = String
                    .format("%s.txt", df.format(new Date()) + "_" + new Random().nextInt(1000));

                byte[] data = file.getBytes();
                FileOutputStream out = new FileOutputStream(new File(dir, fileName));
                IOUtils.write(data, out);
                IOUtils.closeQuietly(out);
            }

            ResultTransport instance = ResultTransport.getErrorBy(ErrorConstant.SUCCESS);

            return instance;

        } catch (Exception e) {
            logger.error("api v1 log upload error: {}", e);
            return ResultTransport.getErrorBy(ErrorConstant.SERVER_ERROR);
        }

    }

    @RequestMapping(value = "insertMember", method = RequestMethod.GET)
    public void insertMember(
        HttpServletRequest req) throws IOException {

        int count = 0;

//		Company company=companyService.getMain();

//		String isphone = "^\\+?\\d{3,13}(\\-?(\\d{6,8}?))?(\\-?(\\d{4,8}?))*$";

        InputStream is = new FileInputStream("C:\\Users\\Administrator\\Desktop\\春诚分公司：驰顺.xls");
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);

//        int row=1;
        List<Passenger> list = new ArrayList<Passenger>();
        // 循环工作表Sheet
        for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
            HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
            if (hssfSheet == null) {
                continue;
            }
            // 循环行Row
            for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
                HSSFRow hssfRow = hssfSheet.getRow(rowNum);
                if (hssfRow == null) {
                    continue;
                }

                String name = null;
                String sex = null;
                String telephone = null;
                String mobile = null;
                String address = null;
                String remark = null;

                for (int cellNum = 0; cellNum <= 5; cellNum++) {

                    HSSFCell cell = hssfRow.getCell(cellNum);

                    if (null != cell) {
                        if (cellNum == 0) {
                            name = getValue(cell);
                        } else if (cellNum == 1) {
                            sex = getValue(cell);
                        } else if (cellNum == 2) {
                            telephone = getValue(cell);
                        } else if (cellNum == 3) {
                            mobile = getValue(cell);
                        } else if (cellNum == 4) {
                            address = getValue(cell);
                        } else if (cellNum == 5) {
                            remark = getValue(cell);
                        }
                    }

                }

                if (null == name) {
                    name = "先生/女士";
                }
                if (null != sex) {
                    if (sex.equals("男")) {
                        sex = "先生";
                    } else if (sex.equals("女")) {
                        sex = "女士";
                    } else {
                        sex = "先生";
                    }
                } else {
                    sex = "先生";
                }

                if (StringUtils.isNotBlank(mobile)) {
                    mobile = mobile.trim();
                } else {
                    if (StringUtils.isNotBlank(telephone)) {
                        mobile = telephone.trim();
                    }
                }

                count++;

                if (StringUtils.isNotBlank(mobile)) {
                    if (StringUtils.isNotBlank(mobile) && null == memberService
                        .findOnePassengerByPhone(mobile)) {
                        Passenger passenger = new Passenger();
                        passenger.setName(name);
                        passenger.setPhone(mobile);
                        passenger.setCompanyId(1L);
                        passenger.setCompanyName("");
                        passenger.setAllowOverdraw(Boolean.FALSE);
                        passenger.setType(Passenger.TYPE_PASSENGER);
                        passenger.setPlateNumber(remark);

                        try {
                            memberService.createPersonal(passenger);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                }

            }

        }
//        memberService.saveList(list);
    }

//	@RequestMapping(value="driverTime",method=RequestMethod.GET)
//	public void paymentsRecord(
//			HttpServletRequest req) throws IOException {
//		
//		Calendar calendar=Calendar.getInstance();
//		calendar.setTime(new Date());
//		String year=String.valueOf(calendar.get(Calendar.YEAR));
//		String month=String.valueOf(calendar.get(Calendar.MONTH)+1);
//		String day=String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
//		
//		calendar.set(Calendar.HOUR_OF_DAY, 0);
//		calendar.set(Calendar.MINUTE, 0);
//		calendar.set(Calendar.SECOND, 0);
//		Date workDate=calendar.getTime();
//		
//		List<Driver> drivers=driverService.findAll();
//		if(!CollectionUtils.isEmpty(drivers)){
//			for (Driver driver : drivers) {
//				if(driver.getStatus().equals(Driver.DRIVER_ONLINE)){
//					DriverTime old=driverTimeService.findOnLine(year, month, day, driver.getId());
//					if(null==old){
//						if(null!=driver.getDutyTime()){
//							
//							Calendar today=Calendar.getInstance();
//							today.setTime(driver.getDutyTime());
//							
//							if(String.valueOf(today.get(Calendar.DAY_OF_MONTH)).equals(day)){
//								DriverTime driverTime=new DriverTime();
//								driverTime.setDriverId(driver.getId());
//								driverTime.setType("online");
//								driverTime.setCreated(driver.getDutyTime());
//								driverTime.setUpdated(driver.getDutyTime());
//								driverTimeService.save(driverTime);
//							}
//							else{
//								DriverTime driverTime=new DriverTime();
//								driverTime.setDriverId(driver.getId());
//								driverTime.setType("online");
//								driverTime.setCreated(workDate);
//								driverTime.setUpdated(workDate);
//								driverTimeService.save(driverTime);
//							}
//							
//						}
//						else{
//							DriverTime driverTime=new DriverTime();
//							driverTime.setDriverId(driver.getId());
//							driverTime.setType("online");
//							driverTime.setCreated(workDate);
//							driverTime.setUpdated(workDate);
//							driverTimeService.save(driverTime);
//						}
//					}
//				}
//				
//			}
//		}
//	}

    @RequestMapping(value = "insertDriverTime", method = RequestMethod.GET)
    public void insertDriverTime(
        HttpServletRequest req) {

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DATE, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date start = calendar.getTime();

        List<DriverTime> driverTimes = driverTimeService.listDriverTimeByDate(start, null);

        if (!CollectionUtils.isEmpty(driverTimes)) {
            for (DriverTime driverTime : driverTimes) {
                if (driverTime.getType().equals("online")) {
                    Driver driver = driverService.findOne(driverTime.getDriverId());
                    if (null != driver) {
                        Calendar c = Calendar.getInstance();
                        c.setTime(driverTime.getOnLineRecored());
                        driverTimeService.driverTimeOnline(driver, c);
                    }
                } else if (driverTime.getType().equals("offline")) {
                    Driver driver = driverService.findOne(driverTime.getDriverId());
                    if (null != driver) {
                        Calendar c = Calendar.getInstance();
                        c.setTime(driverTime.getOffLineRecored());
                        driverTimeService.driverTimeOffline(driver, c);
                    }
                }
            }
        }

    }

    @RequestMapping(value = "insertCoupon", method = RequestMethod.GET)
    public void insertCoupon(
        HttpServletRequest req) {

        List<CouponRule> couponRuleList = couponRuleService.listAllOld();
        if (!CollectionUtils.isEmpty(couponRuleList)) {
            for (CouponRule couponRule : couponRuleList) {
                Result<com.ybkj.daijia.server.sales.CouponRule> newRuleResult =
                    couponRuleService
                        .insert(couponRule.getTitle(), couponRule.getCode(), couponRule.getStart(),
                            couponRule.getEnd(), couponRule.getCompanyId(),
                            com.ybkj.daijia.server.sales.CouponRule.TYPE_DEDUCTION,
                            CouponType.daijia.toString(), null, null, couponRule.getMoney(), 100000,
                            0);
                List<Coupon> couponList = couponService.listAllOldByRule(couponRule.getId());
                if (!CollectionUtils.isEmpty(couponList)) {
                    for (Coupon coupon : couponList) {
                        couponService.insert(coupon.getMemberId(), coupon.getPhone(),
                            newRuleResult.getResult().getId(), coupon.getStatus(), coupon.getCode(),
                            coupon.getRunCode(), coupon.getCodeKey());
                    }
                }
            }
        }

    }

    @RequestMapping(value = "insertDriverLevel", method = RequestMethod.GET)
    public void insertDriverLevel(
        HttpServletRequest req) {

        List<Driver> drivers = driverService.findAll();
        for (Driver driver : drivers) {
            DriverChoseLevel driverChoseLevel = new DriverChoseLevel();
            driverChoseLevel.setDriverId(driver.getId());

            driverChoseLevel.setDriverlevelId(driver.getDriverLevelId());
            if (null != driver.getDriverLevelId()) {
                DriverLevel driverLevel = driverLevelService.findOne(driver.getDriverLevelId());
                if (null != driverLevel) {
                    driverChoseLevel.setCompanyId(driverLevel.getCompanyId());
                }
            }

            if (null != driverChoseLevel.getDriverlevelId() && null != driverChoseLevel
                .getCompanyId()) {
                try {
                    driverChoseLevelService.insertRecord(driverChoseLevel);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }


    @RequestMapping(value = "insertRefuseRecord", method = RequestMethod.GET)
    public void insertRefuseRecord(HttpServletRequest req) {

        List<OrderStatus> orderStatuss = orderStatusService.findAllRefuse();
        if (!CollectionUtils.isEmpty(orderStatuss)) {
            for (OrderStatus orderStatus : orderStatuss) {
                Order order = orderStatus.getOrder();
                if (null != order && null != order.getDriverId()) {
                    Driver driver = driverService.findOne(order.getDriverId());
                    if (null != driver) {
                        DriverRefuseRecord driverRefuseRecord = new DriverRefuseRecord();
                        driverRefuseRecord.setEmployId(driver.getId());
                        driverRefuseRecord.setEmployRealName(driver.getRealname());
                        driverRefuseRecord.setEmployUserName(driver.getUsername());
                        driverRefuseRecord.setEmployPhone(driver.getPhone());

                        driverRefuseRecord.setHappend(orderStatus.getHappened());

                        driverRefuseRecord.setOrderId(order.getId());
                        driverRefuseRecord.setOrderNumber(order.getOrderNumber());
                        driverRefuseRecord.setOrderCompanyId(order.getCompanyId());

                        driverRefuseRecord.setMemo(orderStatus.getMemo());

                        if (order.getOrderCarType().equals(Order.OrderCarType.daijia)) {
                            driverRefuseRecord.setOrderType(DriverRefuseRecord.OrderType.daijia);
                        } else if (order.getOrderCarType().equals(Order.OrderCarType.zhuanche)) {
                            driverRefuseRecord.setOrderType(DriverRefuseRecord.OrderType.zhuanche);
                        }

                        driverRefuseRecordService.save(driverRefuseRecord);
                    }
                }
            }
        }

    }

    @RequestMapping(value = "insertDriverPayments", method = RequestMethod.GET)
    public @ResponseBody
    String insertDriverPayments(HttpServletRequest req) {

        List<DriverRechargeRecord> driverRechargeRecords = driverRechargeRecordService.listAll();
        if (!CollectionUtils.isEmpty(driverRechargeRecords)) {
            for (DriverRechargeRecord driverRechargeRecord : driverRechargeRecords) {
                DriverPaymentsRecord driverPaymentsRecord = new DriverPaymentsRecord();

                Driver driver = driverService.findOne(driverRechargeRecord.getDriverId());
                if (null != driver) {
                    driverPaymentsRecord.setDriverId(driverRechargeRecord.getDriverId());
                    driverPaymentsRecord.setDriverUserName(driver.getUsername());
                    driverPaymentsRecord.setDriverRealName(driver.getRealname());
                    driverPaymentsRecord.setDriverPhone(driver.getPhone());
                    driverPaymentsRecord.setDriverCompanyId(driver.getCompanyId());
                    driverPaymentsRecord.setMoney(driverRechargeRecord.getMoney());
                    driverPaymentsRecord.setAfterMoney(driverRechargeRecord.getAfterMoney());
                    driverPaymentsRecord.setMemo(driverRechargeRecord.getMemo());
                    driverPaymentsRecord.setOperating(driverRechargeRecord.getOperating());
                    driverPaymentsRecord.setRcType(DriverPaymentsRecord.DIFFERENT_POSITIVE);

                    if (null != driverRechargeRecord.getOrderId()) {
                        String orderNumber = driverRechargeRecord.getOrderNumber();
                        Integer orderType = null;
                        Long orderCompanyId = null;

                        if (StringUtils.isNotBlank(orderNumber)) {
                            if (orderNumber.contains("ZC")) {//专车
                                orderType = DriverPaymentsRecord.ORDER_TYPE_ZHUANCHE;
                                com.ybkj.daijia.server.zhuan.Order order = zhuancheService
                                    .findOne(driverRechargeRecord.getOrderId());
                                if (null != order) {
                                    orderCompanyId = order.getCompanyId();
                                }
                            } else if (orderNumber.contains("PT")) {//跑腿
                                orderType = DriverPaymentsRecord.ORDER_TYPE_PAOTUI;
                                com.ybkj.daijia.server.errand.Order order = errandService
                                    .findOne(driverRechargeRecord.getOrderId());
                                if (null != order) {
                                    orderCompanyId = order.getCompanyId();
                                }
                            } else if (orderNumber.contains("HY")) {//货运
                                orderType = DriverPaymentsRecord.ORDER_TYPE_FREIGHT;
                                com.ybkj.daijia.server.freight.Order order = freightService
                                    .findOne(driverRechargeRecord.getOrderId());
                                if (null != order) {
                                    orderCompanyId = order.getCompanyId();
                                }
                            } else {
                                if (orderNumber.length() == 16) {//代驾
                                    orderType = DriverPaymentsRecord.ORDER_TYPE_DAIJIA;
                                    Order order = orderService
                                        .findOne(driverRechargeRecord.getOrderId());
                                    if (null != order) {
                                        orderCompanyId = order.getCompanyId();
                                    }
                                }
                            }
                        } else {
                            orderType = DriverPaymentsRecord.ORDER_TYPE_DAIJIA;
                            Order order = orderService.findOne(driverRechargeRecord.getOrderId());
                            if (null != order) {
                                orderCompanyId = order.getCompanyId();
                                driverRechargeRecord.setOrderNumber(order.getOrderNumber());
                            }
                        }

                        driverPaymentsRecord.setOrderId(driverRechargeRecord.getOrderId());
                        driverPaymentsRecord.setOrderNumber(driverRechargeRecord.getOrderNumber());
                        driverPaymentsRecord.setOrderType(orderType);
                        driverPaymentsRecord.setOrderCompanyId(orderCompanyId);
                    }

                    if (driverRechargeRecord.getRechargeType()
                        .equals(DriverRechargeRecord.RECHARGE_TYPE_ADMIN)) {
                        if (driverRechargeRecord.getMoney().doubleValue() >= 0) {
                            driverPaymentsRecord
                                .setRecordType(DriverPaymentsRecord.RECHARGE_TYPE_ADMIN);
                        } else {
                            driverPaymentsRecord
                                .setRecordType(DriverPaymentsRecord.CONSUMED_TYPE_ADMIN);
                            driverPaymentsRecord.setRcType(DriverPaymentsRecord.DIFFERENT_NEGATIVE);
                            driverPaymentsRecord.setMoney(driverRechargeRecord.getMoney().negate());
                        }
                    } else if (driverRechargeRecord.getRechargeType()
                        .equals(DriverRechargeRecord.RECHARGE_TYPE_ALIPAY)) {
                        driverPaymentsRecord
                            .setRecordType(DriverPaymentsRecord.RECHARGE_TYPE_ALIPAY);
                    } else if (driverRechargeRecord.getRechargeType()
                        .equals(DriverRechargeRecord.RECHARGE_TYPE_WX)) {
                        driverPaymentsRecord.setRecordType(DriverPaymentsRecord.RECHARGE_TYPE_WX);
                    } else if (driverRechargeRecord.getRechargeType()
                        .equals(DriverRechargeRecord.RECHARGE_TYPE_ORDER)) {
                        driverPaymentsRecord
                            .setRecordType(DriverPaymentsRecord.RECHARGE_TYPE_ORDER);
                    } else if (driverRechargeRecord.getRechargeType()
                        .equals(DriverRechargeRecord.RECHARGE_TYPE_ORDER_BALANCE)) {
                        driverPaymentsRecord
                            .setRecordType(DriverPaymentsRecord.RECHARGE_TYPE_ORDER);
                    } else if (driverRechargeRecord.getRechargeType()
                        .equals(DriverRechargeRecord.RECHARGE_TYPE_BAOXIAO)) {
                        driverPaymentsRecord
                            .setRecordType(DriverPaymentsRecord.RECHARGE_TYPE_BAOXIAO);
                    } else if (driverRechargeRecord.getRechargeType()
                        .equals(DriverRechargeRecord.RECHARGE_TYPE_MODIFY)) {
                        driverPaymentsRecord
                            .setRecordType(DriverPaymentsRecord.RECHARGE_TYPE_MODIFY);
                    } else if (driverRechargeRecord.getRechargeType()
                        .equals(DriverRechargeRecord.RECHARGE_TYPE_REBATE)) {
                        driverPaymentsRecord
                            .setRecordType(DriverPaymentsRecord.RECHARGE_TYPE_REBATE);
                    } else if (driverRechargeRecord.getRechargeType()
                        .equals(DriverRechargeRecord.RECHARGE_TYPE_ORDER_GUOLU)) {
                        driverPaymentsRecord
                            .setRecordType(DriverPaymentsRecord.RECHARGE_TYPE_ORDER_GUOLU);
                    }

                    driverPaymentsRecord.setHappend(driverRechargeRecord.getCreated());
                    driverPaymentsRecordService.insertRecord(driverPaymentsRecord);

                }

            }

        }

        List<DriverConsumedRecord> driverConsumedRecords = driverConsumedRecordService.listAll();
        if (!CollectionUtils.isEmpty(driverConsumedRecords)) {
            for (DriverConsumedRecord driverConsumedRecord : driverConsumedRecords) {
                DriverPaymentsRecord driverPaymentsRecord = new DriverPaymentsRecord();

                Driver driver = driverService.findOne(driverConsumedRecord.getDriverId());
                if (null != driver) {
                    driverPaymentsRecord.setDriverId(driverConsumedRecord.getDriverId());
                    driverPaymentsRecord.setDriverUserName(driver.getUsername());
                    driverPaymentsRecord.setDriverRealName(driver.getRealname());
                    driverPaymentsRecord.setDriverPhone(driver.getPhone());
                    driverPaymentsRecord.setDriverCompanyId(driver.getCompanyId());
                    driverPaymentsRecord.setMoney(driverConsumedRecord.getMoney());
                    driverPaymentsRecord.setAfterMoney(driverConsumedRecord.getAfterMoney());
                    driverPaymentsRecord.setMemo(driverConsumedRecord.getMemo());
                    driverPaymentsRecord.setOperating(DriverPaymentsRecord.OPERATING_SYS);
                    driverPaymentsRecord.setRcType(DriverPaymentsRecord.DIFFERENT_NEGATIVE);

                    if (null != driverConsumedRecord.getOrderId()) {
                        String orderNumber = driverConsumedRecord.getOrderNumber();
                        Integer orderType = null;
                        Long orderCompanyId = null;

                        if (StringUtils.isNotBlank(orderNumber)) {
                            if (orderNumber.contains("ZC")) {//专车
                                orderType = DriverPaymentsRecord.ORDER_TYPE_ZHUANCHE;
                                com.ybkj.daijia.server.zhuan.Order order = zhuancheService
                                    .findOne(driverConsumedRecord.getOrderId());
                                if (null != order) {
                                    orderCompanyId = order.getCompanyId();
                                }
                            } else if (orderNumber.contains("PT")) {//跑腿
                                orderType = DriverPaymentsRecord.ORDER_TYPE_PAOTUI;
                                com.ybkj.daijia.server.errand.Order order = errandService
                                    .findOne(driverConsumedRecord.getOrderId());
                                if (null != order) {
                                    orderCompanyId = order.getCompanyId();
                                }
                            } else if (orderNumber.contains("HY")) {//货运
                                orderType = DriverPaymentsRecord.ORDER_TYPE_FREIGHT;
                                com.ybkj.daijia.server.freight.Order order = freightService
                                    .findOne(driverConsumedRecord.getOrderId());
                                if (null != order) {
                                    orderCompanyId = order.getCompanyId();
                                }
                            } else {
                                if (orderNumber.length() == 16) {//代驾
                                    orderType = DriverPaymentsRecord.ORDER_TYPE_DAIJIA;
                                    Order order = orderService
                                        .findOne(driverConsumedRecord.getOrderId());
                                    if (null != order) {
                                        orderCompanyId = order.getCompanyId();
                                    }
                                }
                            }
                        } else {
                            orderType = DriverPaymentsRecord.ORDER_TYPE_DAIJIA;
                            Order order = orderService.findOne(driverConsumedRecord.getOrderId());
                            if (null != order) {
                                orderCompanyId = order.getCompanyId();
                                driverConsumedRecord.setOrderNumber(order.getOrderNumber());
                            }
                        }

                        driverPaymentsRecord.setOrderId(driverConsumedRecord.getOrderId());
                        driverPaymentsRecord.setOrderNumber(driverConsumedRecord.getOrderNumber());
                        driverPaymentsRecord.setOrderType(orderType);
                        driverPaymentsRecord.setOrderCompanyId(orderCompanyId);

                        if (driverConsumedRecord.getType()
                            .equals(DriverConsumedRecord.DRIVER_CONSUMEDRECORD_TYPE_ORDER)) {
                            driverPaymentsRecord
                                .setRecordType(DriverPaymentsRecord.CONSUMED_TYPE_ORDER);
                        } else if (driverConsumedRecord.getType().equals("提成")) {
                            driverPaymentsRecord
                                .setRecordType(DriverPaymentsRecord.CONSUMED_TYPE_ORDER);
                        } else if (driverConsumedRecord.getType()
                            .equals(DriverConsumedRecord.DRIVER_CONSUMEDRECORD_TYPE_XINXI)) {
                            driverPaymentsRecord
                                .setRecordType(DriverPaymentsRecord.CONSUMEDRECORD_TYPE_XINXI);
                        } else if (driverConsumedRecord.getType()
                            .equals(DriverConsumedRecord.DRIVER_CONSUMEDRECORD_HELPPAY)) {
                            driverPaymentsRecord
                                .setRecordType(DriverPaymentsRecord.CONSUMEDRECORD_TYPE_HELPPAY);
                        } else if (driverConsumedRecord.getType()
                            .equals(DriverConsumedRecord.DRIVER_CONSUMEDRECORDWORK_WORKCAR)) {
                            driverPaymentsRecord
                                .setRecordType(DriverPaymentsRecord.CONSUMEDRECORD_TYPE_WORKCAR);
                        }

                        driverPaymentsRecord.setHappend(driverConsumedRecord.getCreated());
                        driverPaymentsRecordService.insertRecord(driverPaymentsRecord);

                    }

                }
            }
        }
        return "success";

    }

    private String getBasePath(HttpServletRequest req) {

        try {
            return WebUtils.getRealPath(req.getSession().getServletContext(), "/");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        return StringUtils.EMPTY;
    }

//	@RequestMapping(value="initMember",method=RequestMethod.GET)
//	private void initMemberAlreadyMoney(){
//		List<Passenger> list = memberService.findAllPassengersByTypeAndCompany(null, null);
//		
//		for (int i = 0; i < list.size(); i++) {
//			BigDecimal alreadyMoney = BigDecimal.ZERO;
//			Member member = list.get(i);
//			List<Order> orderList = orderService.findByMemberId(member.getId());
//			for (int j = 0; j < orderList.size(); j++) {
//				if(null!=orderList.get(j).getCashMoney()){
//					alreadyMoney= alreadyMoney.add(orderList.get(j).getCashMoney());
//				}
//				if(null!=orderList.get(j).getSignMoney()){
//					alreadyMoney= alreadyMoney.add(orderList.get(j).getSignMoney());
//				}
//				if(null!=orderList.get(j).getBalanceMoney()){
//					alreadyMoney= alreadyMoney.add(orderList.get(j).getBalanceMoney());
//				}
//			}
//			member.setAlreadyMoney(alreadyMoney);
//			memberService.updateByAlreadyMoney(member);
//			
//		}
//	}


    @RequestMapping(value = "wxtest", method = RequestMethod.GET)
    private void wxtest(String phone) {
//		Member member=new Member();
//		member.setName("先生/女士");
//		member.setType(Member.MEMBER_TYPE_COMMON);
//		member.setFromSource("微信");
//		member.setIsOverdraw(Boolean.FALSE);
//		member.setVirtual(BigDecimal.ZERO);
//		
//		Company company=companyService.getMain();
//		member.setCompanyId(company.getId());
//		memberService.insertRecord(member);
//		
//		MemberAccount memberAccount=new MemberAccount();
//		memberAccount.setName("先生/女士");
//		memberAccount.setPhone(phone);
//		memberAccount.setMemberId(member.getId());
//		memberAccountService.insertRecord(memberAccount);
//		
//		//新注册活动
//		ReactorVo rVo=new ReactorVo();
//		rVo.setMember(member);
//		rVo.setPhone(phone);
//		rVo.setActivityEvent(ActivityEvent.newRegister);
//		ActivityCheckEvent activityCheckEvent = new ActivityCheckEvent(rVo);
//		applicationEventMulticaster.multicastEvent(activityCheckEvent);
    }


    @RequestMapping(value = "newOrderInfo", method = RequestMethod.GET)
    private @ResponseBody
    String newOrderInfo() {

        taskExecutor.execute(new Runnable() {
            public void run() {

                String error = "";
                Long errorCount = 0L;

                Long totalOrderAmount = orderService.countAllFinished();

                Integer limit = 100;
                Integer pages = 0;
                if (totalOrderAmount % 100 == 0) {
                    pages = (int) (totalOrderAmount / 100);
                } else {
                    pages = (int) (totalOrderAmount / 100) + 1;
                }

                for (int page = 0; page < pages; page++) {
                    List<Order> orderList = orderService.findAllFinished(page, limit);
                    if (!CollectionUtils.isEmpty(orderList)) {
                        for (Order order : orderList) {
                            try {

                                OrderFinishedInfo orderFinishedInfo = new OrderFinishedInfo();
                                orderFinishedInfo.setOrderId(order.getId());
                                orderFinishedInfo.setOrderNumber(order.getOrderNumber());
                                orderFinishedInfo.setFinishTime(order.getFinishTime());
                                orderFinishedInfo.setCreateTime(order.getCreated());
                                orderFinishedInfo.setClientId(order.getClientId());
                                orderFinishedInfo.setClientPhone(order.getClientPhone());
                                orderFinishedInfo.setDriverId(order.getDriverId());
                                orderFinishedInfo.setCompanyId(order.getCompanyId());
                                if (null != order.getPayTypeDetails()) {
                                    orderFinishedInfo.setPayTypeDetails(order.getPayTypeDetails());
                                } else {
                                    if (order.getPayType().equals(Order.PayType.cash)) {
                                        orderFinishedInfo
                                            .setPayTypeDetails(Order.PayTypeDetails.cash);
                                        orderFinishedInfo.setCashMoney(order.getRealCash());
                                    } else if (order.getPayType().equals(Order.PayType.sign)) {
                                        orderFinishedInfo
                                            .setPayTypeDetails(Order.PayTypeDetails.sign);
                                        orderFinishedInfo.setSignMoney(order.getRealCash());
                                    } else if (order.getPayType().equals(Order.PayType.balance)) {
                                        orderFinishedInfo
                                            .setPayTypeDetails(Order.PayTypeDetails.balance);
                                        orderFinishedInfo.setBalanceMoney(order.getRealCash());
                                    }
                                }

                                orderFinishedInfo.setShouldCash(order.getShouldCash());
                                orderFinishedInfo.setRealCash(order.getRealCash());
                                orderFinishedInfo.setCouponMoney(order.getCouponMoney());
                                orderFinishedInfo.setCashMoney(order.getCashMoney());
                                orderFinishedInfo.setSignMoney(order.getSignMoney());
                                orderFinishedInfo.setBalanceMoney(order.getBalanceMoney());

                                //信息费
                                List<DriverConsumedRecord> infoFeeList = driverConsumedRecordService
                                    .findByTypeAndOrderId(
                                        DriverConsumedRecord.DRIVER_CONSUMEDRECORD_TYPE_XINXI,
                                        order.getId());
                                if (!CollectionUtils.isEmpty(infoFeeList)) {
                                    for (DriverConsumedRecord driverConsumedRecord : infoFeeList) {
                                        orderFinishedInfo.setInfoFee(orderFinishedInfo.getInfoFee()
                                            .add(driverConsumedRecord.getMoney()));
                                    }
                                }

                                //推荐提成
                                DriverRechargeRecord driverRebate = driverRechargeRecordService
                                    .findByTypeAndOrderId(DriverRechargeRecord.RECHARGE_TYPE_REBATE,
                                        order.getId());
                                if (null != driverRebate) {//司机提成
                                    orderFinishedInfo.setRebateMoney(driverRebate.getMoney());
                                } else {
                                    //客户提成

                                    StringBuilder sb = new StringBuilder();
                                    sb.append(passengerApi.getPassengerBaseURL()
                                        + PassengerApi.PASSENGER_ACCOUNT_ORDER
                                        + "?appKey={appKey}&appId={appId}&sign={sign}");
                                    sb.append("&orderId={orderId}");
                                    sb.append("&payType={payType}");

                                    Map<String, String> kv = new HashMap<String, String>();
                                    kv.put("orderId", String.valueOf(order.getId()));
                                    kv.put("payType", String
                                        .valueOf(PassengerAccount.PayType.RECHARGE_TYPE_REBATE));

                                    String result = passengerApi
                                        .get(sb.toString(), kv, settingUtils.get());

                                    List<PassengerAccount> passengerAccounts = new ArrayList<PassengerAccount>();

                                    if (null != result) {
                                        JSONObject jo = new JSONObject(result);
                                        Integer code = jo.getInt("code");
                                        if (code == 0) {
                                            JSONObject data = jo.getJSONObject("data");
                                            JSONArray ja = data.getJSONArray("rows");
                                            Gson gson = new Gson();
                                            passengerAccounts = gson.fromJson(ja.toString(),
                                                new TypeToken<List<PassengerAccount>>() {
                                                }.getType());
                                        }
                                    }

                                    if (!CollectionUtils.isEmpty(passengerAccounts)) {
                                        for (PassengerAccount passengerAccount : passengerAccounts) {
                                            orderFinishedInfo.setRebateMoney(
                                                orderFinishedInfo.getRebateMoney().add(
                                                    new BigDecimal(passengerAccount.getMoney())));
                                        }
                                    }

//									List<PassengerAccount> passengerAccounts=memberPaymentsRecordService.findMoreByTypeAndOrderId(MemberPaymentsRecord.PaymentType.RECHARGE_TYPE_REBATE.toString(), order.getId());
//									if(!CollectionUtils.isEmpty(memberRebates)){
//										for (MemberPaymentsRecord memberRebate : memberRebates) {
//											orderFinishedInfo.setRebateMoney(orderFinishedInfo.getRebateMoney().add(memberRebate.getMoney()));
//										}
//									}
                                }

                                //客户经理
                                WeixinManagerXOrder weixinManagerXOrder = weixinManagerXOrderService
                                    .findByOrderId(order.getId());
                                if (null != weixinManagerXOrder && null != weixinManagerXOrder
                                    .getFinishedTime()) {
                                    orderFinishedInfo
                                        .setWxManagerMoney(weixinManagerXOrder.getMoney());
                                }

                                //现金
                                if (orderFinishedInfo.getPayTypeDetails()
                                    .equals(Order.PayTypeDetails.cash)) {
                                    List<DriverConsumedRecord> driverConsumedRecords = driverConsumedRecordService
                                        .findByTypeAndOrderId(
                                            DriverConsumedRecord.DRIVER_CONSUMEDRECORD_TYPE_ORDER,
                                            order.getId());
                                    if (!CollectionUtils.isEmpty(driverConsumedRecords)) {
                                        if (null != order.getCashMoney()) {
                                            DriverConsumedRecord driverConsumedRecord = driverConsumedRecords
                                                .get(0);
                                            orderFinishedInfo
                                                .setDriverMoney(driverConsumedRecord.getTicheng());
                                            orderFinishedInfo.setCompanyMoney(order.getCashMoney()
                                                .subtract(orderFinishedInfo.getDriverMoney())
                                                .subtract(orderFinishedInfo.getWxManagerMoney()));
                                        } else {
                                            DriverConsumedRecord driverConsumedRecord = driverConsumedRecords
                                                .get(0);
                                            orderFinishedInfo
                                                .setDriverMoney(driverConsumedRecord.getTicheng());
                                            orderFinishedInfo.setCompanyMoney(order.getRealCash()
                                                .subtract(orderFinishedInfo.getDriverMoney())
                                                .subtract(orderFinishedInfo.getWxManagerMoney()));
                                        }
                                    }
                                }//现金+优惠券
                                else if (orderFinishedInfo.getPayTypeDetails()
                                    .equals(Order.PayTypeDetails.coupon_cash)) {
                                    List<DriverConsumedRecord> driverConsumedRecords = driverConsumedRecordService
                                        .findByTypeAndOrderId(
                                            DriverConsumedRecord.DRIVER_CONSUMEDRECORD_TYPE_ORDER,
                                            order.getId());
                                    if (!CollectionUtils.isEmpty(driverConsumedRecords)) {
                                        DriverConsumedRecord driverConsumedRecord = driverConsumedRecords
                                            .get(0);
                                        orderFinishedInfo
                                            .setDriverMoney(driverConsumedRecord.getTicheng());
                                        orderFinishedInfo.setCompanyMoney(order.getCashMoney()
                                            .subtract(orderFinishedInfo.getDriverMoney())
                                            .subtract(orderFinishedInfo.getWxManagerMoney()));
                                    } else {
                                        DriverRechargeRecord driverRechargeRecord = driverRechargeRecordService
                                            .findByTypeAndOrderId(
                                                DriverRechargeRecord.RECHARGE_TYPE_ORDER,
                                                order.getId());
                                        if (null != driverRechargeRecord) {
                                            orderFinishedInfo.setDriverMoney(
                                                driverRechargeRecord.getMoney()
                                                    .add(order.getCashMoney()));
                                            orderFinishedInfo.setCompanyMoney(order.getCashMoney()
                                                .subtract(orderFinishedInfo.getDriverMoney())
                                                .subtract(orderFinishedInfo.getWxManagerMoney()));
                                        }
                                    }
                                }//签单
                                else if (orderFinishedInfo.getPayTypeDetails()
                                    .equals(Order.PayTypeDetails.sign)) {
                                    DriverRechargeRecord driverRechargeRecord = driverRechargeRecordService
                                        .findByTypeAndOrderId(
                                            DriverRechargeRecord.RECHARGE_TYPE_ORDER,
                                            order.getId());
                                    if (null != driverRechargeRecord) {
                                        if (null != order.getSignMoney()) {
                                            orderFinishedInfo
                                                .setDriverMoney(driverRechargeRecord.getMoney());
                                            orderFinishedInfo.setCompanyMoney(order.getSignMoney()
                                                .subtract(orderFinishedInfo.getDriverMoney())
                                                .subtract(orderFinishedInfo.getWxManagerMoney()));
                                        } else {
                                            orderFinishedInfo
                                                .setDriverMoney(driverRechargeRecord.getMoney());
                                            orderFinishedInfo.setCompanyMoney(order.getRealCash()
                                                .subtract(orderFinishedInfo.getDriverMoney())
                                                .subtract(orderFinishedInfo.getWxManagerMoney()));
                                        }
                                    }
                                }//签单+优惠券
                                else if (orderFinishedInfo.getPayTypeDetails()
                                    .equals(Order.PayTypeDetails.coupon_sign)) {
                                    DriverRechargeRecord driverRechargeRecord = driverRechargeRecordService
                                        .findByTypeAndOrderId(
                                            DriverRechargeRecord.RECHARGE_TYPE_ORDER,
                                            order.getId());
                                    if (null != driverRechargeRecord) {
                                        orderFinishedInfo
                                            .setDriverMoney(driverRechargeRecord.getMoney());
                                        orderFinishedInfo.setCompanyMoney(order.getSignMoney()
                                            .subtract(orderFinishedInfo.getDriverMoney())
                                            .subtract(orderFinishedInfo.getWxManagerMoney()));
                                    }
                                }//余额
                                else if (orderFinishedInfo.getPayTypeDetails()
                                    .equals(Order.PayTypeDetails.balance)) {
                                    DriverRechargeRecord driverRechargeRecord = driverRechargeRecordService
                                        .findByTypeAndOrderId(
                                            DriverRechargeRecord.RECHARGE_TYPE_ORDER,
                                            order.getId());
                                    if (null != driverRechargeRecord) {
                                        if (null != order.getBalanceMoney()) {
                                            orderFinishedInfo
                                                .setDriverMoney(driverRechargeRecord.getMoney());
                                            orderFinishedInfo.setCompanyMoney(
                                                order.getBalanceMoney()
                                                    .subtract(orderFinishedInfo.getDriverMoney())
                                                    .subtract(
                                                        orderFinishedInfo.getWxManagerMoney()));
                                        } else {
                                            orderFinishedInfo
                                                .setDriverMoney(driverRechargeRecord.getMoney());
                                            orderFinishedInfo.setCompanyMoney(order.getRealCash()
                                                .subtract(orderFinishedInfo.getDriverMoney())
                                                .subtract(orderFinishedInfo.getWxManagerMoney()));
                                        }
                                    }
                                }//余额+优惠券
                                else if (orderFinishedInfo.getPayTypeDetails()
                                    .equals(Order.PayTypeDetails.coupon_balance)) {
                                    DriverRechargeRecord driverRechargeRecord = driverRechargeRecordService
                                        .findByTypeAndOrderId(
                                            DriverRechargeRecord.RECHARGE_TYPE_ORDER,
                                            order.getId());
                                    if (null != driverRechargeRecord) {
                                        orderFinishedInfo
                                            .setDriverMoney(driverRechargeRecord.getMoney());
                                        orderFinishedInfo.setCompanyMoney(order.getBalanceMoney()
                                            .subtract(orderFinishedInfo.getDriverMoney())
                                            .subtract(orderFinishedInfo.getWxManagerMoney()));
                                    }
                                }//余额+现金
                                else if (orderFinishedInfo.getPayTypeDetails()
                                    .equals(Order.PayTypeDetails.balance_cash)) {
                                    //司机提成<手里现金 司机扣费
                                    List<DriverConsumedRecord> driverConsumedRecords = driverConsumedRecordService
                                        .findByTypeAndOrderId(
                                            DriverConsumedRecord.DRIVER_CONSUMEDRECORD_TYPE_ORDER,
                                            order.getId());
                                    if (!CollectionUtils.isEmpty(driverConsumedRecords)) {
                                        DriverConsumedRecord driverConsumedRecord = driverConsumedRecords
                                            .get(0);
                                        orderFinishedInfo
                                            .setDriverMoney(driverConsumedRecord.getTicheng());
                                        orderFinishedInfo.setCompanyMoney(
                                            driverConsumedRecord.getMoney()
                                                .multiply(new BigDecimal(-1)).add(
                                                order.getBalanceMoney().subtract(
                                                    orderFinishedInfo.getWxManagerMoney())));
                                    } else {
                                        //司机提成>手里现金 司机加钱
                                        DriverRechargeRecord driverRechargeRecord = driverRechargeRecordService
                                            .findByTypeAndOrderId(
                                                DriverRechargeRecord.RECHARGE_TYPE_ORDER,
                                                order.getId());
                                        if (null != driverRechargeRecord) {
                                            orderFinishedInfo.setDriverMoney(order.getCashMoney()
                                                .add(driverRechargeRecord.getMoney()));
                                            orderFinishedInfo.setCompanyMoney(
                                                order.getBalanceMoney()
                                                    .subtract(driverRechargeRecord.getMoney())
                                                    .subtract(
                                                        orderFinishedInfo.getWxManagerMoney()));
                                        }
                                    }
                                }

                                orderFinishedInfoService.insertRecord(orderFinishedInfo);

                            } catch (Exception e) {
                                errorCount++;
                                error += order.getId() + " ";
                                e.printStackTrace();
                            }

                        }
                    }

                    if (errorCount != 0) {
                        logger.error("failure: {},orderid: {}", errorCount, error);
                    }

                }
            }
        });

        return "success";

    }

    @RequestMapping(value = "updatePaotuiOrder", method = RequestMethod.GET)
    private @ResponseBody
    String updatePaotuiOrder() {

        List<FinancialRecord> financialRecords = financialRecordService
            .listByOrderType(FinancialRecord.ORDER_TYPE_PAOTUI);
        String error = "";
        if (!CollectionUtils.isEmpty(financialRecords)) {

            StringBuilder sb = new StringBuilder();
            sb.append(errandApi.getErrandBaseURL() + "api/orders/initChartOrder"
                + "?appKey={appKey}&appId={appId}&sign={sign}");

            for (FinancialRecord financialRecord : financialRecords) {
                Long orderId = financialRecord.getOrderId();
                Long driverCompanyId = financialRecord.getCompanyId();
                BigDecimal companyProfit = financialRecord.getMoney();

                Map<String, String> kv = new HashMap<String, String>();

                kv.put("orderId", String.valueOf(orderId));
                sb.append("&orderId={orderId}");
                kv.put("driverCompanyId", String.valueOf(driverCompanyId));
                sb.append("&driverCompanyId={driverCompanyId}");
                kv.put("companyProfit", String.valueOf(companyProfit.doubleValue()));
                sb.append("&companyProfit={companyProfit}");

                String resultStr = errandApi.post(sb.toString(), kv, settingUtils.get());

                if (null != resultStr) {
                    JSONObject jo = new JSONObject(resultStr);
                    Integer code = jo.getInt("code");
                    if (code == 0) {

                    } else {
                        String message = jo.getString("message");
                        error += "orderId:" + orderId + ",message:" + message + "|";
                    }
                }

            }
        }

        if (StringUtils.isNotBlank(error)) {
            return error;
        }
        return "success";
    }

    @RequestMapping(value = "testSms", method = RequestMethod.GET)
    public @ResponseBody
    void testSms() {

//		for (int i = 0; i < 3; i++) {

        taskExecutor.execute(new Runnable() {
            public void run() {
                try {
                    smsApi.batchSendSms("18208160972,15108206284,15102875535", "您的验证码是：1010【小咖科技】");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

//		}

//	  try {
//        smsApi.batchSendSms("18208160972,18208160972,18208160972,18208160972", "您的验证码是：7788【小咔科技】");
//      } catch (Exception e) {
//          e.printStackTrace();
//      }
    }

    @RequestMapping(value = "initAdmin", method = RequestMethod.GET)
    public @ResponseBody
    String initAdmin(String u, String p) {
        Admin admin = adminService.findByUsername(u);

        if (null == admin) {
            return "admin null";
        }

        String p_md5 = MD5Util.MD5Encode(p, null);

        Base64 base64 = new Base64();
        p_md5 = base64.encode(p_md5.getBytes());

        Sha256Hash hash = new Sha256Hash(p_md5, Admin.SALT_KEY);

        admin.setPassword(hash.toBase64());
        adminService.updateByPrimaryKey(admin);

        return "success";
    }

//	@RequestMapping(value = "shareSuccess", method = RequestMethod.GET)
//    public synchronized @ResponseBody ResultTransport shareSuccess(
//            String phone, String deviceType, SharePlatform sharePlatform) {
//
//        logger.debug("recv phone:{},deviceType:{},sharePlatform:{}", phone, deviceType, sharePlatform);
//
//        if (StringUtils.isBlank(phone) || StringUtils.isBlank(deviceType) || null==sharePlatform) {
//            return ResultTransport.getErrorBy(ErrorConstant.PARAM_ERROR);
//        }
//
//        Passenger passenger = memberService.findOnePassengerByPhone(phone);
//
//        if (null == passenger) {
//            return ResultTransport.getErrorBy(ErrorConstant.MEMBER_NOT_EXISTS_ERROR);
//        }
//
//        try {
//
//            ShareRecord shareRecord = new ShareRecord();
//            shareRecord.setDeviceType(deviceType);
//            shareRecord.setMemberId(passenger.getId());
//            shareRecord.setPhone(phone);
//            shareRecord.setSharePlatform(sharePlatform);
//
//            ReactorVo rVo = new ReactorVo();
//            rVo.setPassenger(passenger);
//            rVo.setActivityEvent(ActivityEvent.shareSuccess);
//            rVo.setShareRecord(shareRecord);
//            ActivityCheckEvent activityCheckEvent = new ActivityCheckEvent(rVo);
//            applicationEventMulticaster.multicastEvent(activityCheckEvent);
//
//            ResultTransport instance = ResultTransport.getErrorBy(ErrorConstant.SUCCESS);
//
//            return instance;
//
//        } catch (Exception e) {
//            logger.error("api v3 member shareSuccess error: {}", e);
//            return ResultTransport.getErrorBy(ErrorConstant.SERVER_ERROR);
//        }
//
//    }

}
